<script setup lang="ts">
import { ref } from 'vue';
import { $t } from '@/locales';
import { businessTypeOptions } from '@/constants/business'; // 需确认业务类型选项
import { translateOptions } from '@/utils/common';
import { useForm } from '@/hooks/common/form';
defineOptions({ name: 'LogSearch' }); // 修改组件名称为日志搜索

const activeName = ref(['log-search']);

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}
const { validate } = useForm();
const emit = defineEmits<Emits>();

// 使用日志搜索参数类型（根据api.d.ts定义）
const model = defineModel<Api.SystemManage.LogSearchParams>('model', { required: true });

function reset() {
  if (model.value) {
    // 重置日志搜索参数
    if ('businessType' in model.value) model.value.businessType = undefined; // 业务类型
    if ('createBy' in model.value) model.value.createBy = undefined;        // 创建人
    if ('createTime' in model.value) model.value.createTime = undefined;    // 创建时间范围
    if ('updateTime' in model.value) model.value.updateTime = undefined;    // 更新时间范围
  }
  emit('reset');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <ElCard class="card-wrapper">
    <ElCollapse v-model="activeName">
      <ElForm>
        <ElRow :gutter="24" align="middle" justify="center" class="items-center">
          <!-- 业务类型选择 -->
          <ElCol :lg="6" :md="8" :sm="12">
            <ElFormItem :label="$t('page.manage.log.businessType')" prop="businessType">
              <ElSelect v-model="model.businessType" :placeholder="$t('page.manage.log.form.businessType')" clearable>
                <ElOption
                  v-for="{ label, value } in translateOptions(businessTypeOptions)"
                  :key="value"
                  :label="label"
                  :value="value"
                ></ElOption>
              </ElSelect>
            </ElFormItem>
          </ElCol>

          <!-- 创建人输入 -->
          <ElCol :lg="6" :md="8" :sm="12">
            <ElFormItem :label="$t('page.manage.log.createBy')" prop="createBy">
              <ElInput v-model="model.createBy" :placeholder="$t('page.manage.log.form.createBy')" />
            </ElFormItem>
          </ElCol>

          <!-- 时间范围选择 -->
          <ElCol :lg="8" :md="12" :sm="24">
            <ElFormItem :label="$t('page.manage.log.timeRange')">
              <ElDatePicker
                v-model="model.createTime"
                type="datetimerange"
                range-separator="-"
                :start-placeholder="$t('page.manage.log.startTime')"
                :end-placeholder="$t('page.manage.log.endTime')"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </ElFormItem>
          </ElCol>

          <!-- 操作按钮 -->
          <ElCol :lg="6" :md="24" :sm="24">
            <ElFormItem>
              <ElSpace class="w-full justify-end flex items-center" alignment="center">
                <ElButton @click="reset">
                  <template #icon>
                    <icon-ic-round-refresh class="text-icon" />
                  </template>
                  {{ $t('common.reset') }}
                </ElButton>
                <ElButton type="primary" plain @click="search">
                  <template #icon>
                    <icon-ic-round-search class="text-icon" />
                  </template>
                  {{ $t('common.search') }}
                </ElButton>
              </ElSpace>
            </ElFormItem>
          </ElCol>
        </ElRow>
      </ElForm>
    </ElCollapse>
  </ElCard>
</template>

<!-- 保留原有样式 -->
<style scoped>
.items-center {
  display: flex;
  align-items: center;
}

:deep(.el-form-item) {
  margin-bottom: 0;
}

:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
}
</style>
